package hottargui.config;

import hottargui.framework.*;
import hottargui.standard.StandardGame;

public class BetaDefendState implements TarguiState {

	Game gameContext;
	Position basePos;
	Position targetPos;
	
	public BetaDefendState(Game game, Position base, Position target)
	{
		gameContext = game;
		basePos = base;
		targetPos = target;
	}
	
	public boolean buy(int count, Position deploy) throws IllegalActionException {
		throw new IllegalActionException(this.getState());
	}

	public State getState() {
		return State.defend;
	}

	public PlayerColor getWinner() throws IllegalActionException {
		throw new IllegalActionException(this.getState());	
	}

	public boolean move(Position from, Position to, int count)
			throws IllegalActionException {
		throw new IllegalActionException(this.getState());	
	}

	public void newGame() throws IllegalActionException {
		throw new IllegalActionException(this.getState());	
	}

	public void rollDie() throws IllegalActionException {
		AttackStrategy as = gameContext.getGameObjectServer().getAttackStrategy();
		DiceRollStrategy drs = gameContext.getGameObjectServer().getDiceRollStrategy();
		
		int die = drs.rollDie();
		gameContext.getGameObjectServer().getGameData().setLastDieRoll(die);
		
		Tile baseTile = gameContext.getTile(basePos);
		Tile targetTile = gameContext.getTile(targetPos);
		
		int killed = as.attack(targetTile, baseTile, die);
		((StandardGame)gameContext).report("Defense killed "+killed+" enemy units");
		
		// Notice: tiles are turned around from attackstate as we are defending
		baseTile.setUnitCount(baseTile.getUnitCount() - killed);
		
		if ( baseTile.getUnitCount() == 0 )
		{
			((StandardGame)gameContext).report("Enemy attack was beaten!");
			gameContext.setState(gameContext.getFactory().getBuyState(gameContext));
		}
		else
		{
			gameContext.setState(gameContext.getFactory().getAttackState(gameContext, basePos, targetPos));
		}
	}

	public PlayerColor turnCard() throws IllegalActionException {
		throw new IllegalActionException(this.getState());
	}
	
	public void yieldAttack() throws IllegalActionException
	{
		throw new IllegalActionException(this.getState());
	}
	
	public void skip() throws IllegalActionException
	{
		throw new IllegalActionException(this.getState());
	}
}
